home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / diskio.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  191 lines

  1. #define ULAW
  2.  
  3. #ifdef ULAW
  4. #include "../ulaw2linear.h"
  5. #endif
  6.  
  7. /******************************************************************
  8. *
  9. *    DISKIO Version 49
  10. *
  11. ******************************************************************
  12. *
  13. *    Handle I/O to sampled data (speech) files
  14. *
  15. *      Uses FORTRAN sequential access, unformatted files with
  16. *      signed 16 bit samples, 64 samples per record.
  17. *
  18. *   DISKRD - Read speech from disk file
  19. *   DISKWR - Write speech to disk file
  20. *      CHAN   - Channel number (1 to MAXC)
  21. *      SPEECH - REAL buffer of samples (-1.0 to 1.0)
  22. *      LENGTH - Number of samples to read or write
  23. *        if ZERO, then close input or output file
  24. *      EOF    - If TRUE, end of file encountered on input
  25. *
  26. *   DISKOP - Open a speech file for read or write
  27. *      CHAN   - Channel number (1 to MAXC)
  28. *      FUNCTN - 0 for read, 1 for write
  29. *      FNAME  - File name
  30. *
  31. *   DISKCL - Close a speech file
  32. *      CHAN   - Channel number (1 to MAXC)
  33. *
  34. *   DISKFN - Get the filename of an open speech file
  35. *      CHAN   - Channel number (1 to MAXC)
  36. *      FNAME  - File name
  37. *      FLEN   - Number of characters in FNAME
  38. *
  39. *   NOTE: Functions DISKOP and DISKCL are the preferred means
  40. *   for opening and closing speech files.  If DISKOP is not called
  41. *   prior to DISKRD or DISKWR, the user is prompted for a filename.
  42. *   DISKCL should be called for output files to ensure that the final
  43. *   record is written.
  44. */
  45.  
  46. #include "lpcdefs.h"
  47.  
  48. #define RECLEN 8192
  49. #define maxc 4 
  50. #define rd 0
  51. #define wr 1
  52.  
  53. diskio(which, chan, speech, length, eof, fname, functn)
  54. int which, length, functn;
  55. float *speech;
  56. char *fname;
  57. short *eof;
  58. FILE *chan;
  59. {
  60. int i,k;
  61. #ifdef ULAW
  62. unsigned char buf[RECLEN / 2];
  63. #else
  64. short buf[RECLEN];
  65. #endif
  66. #ifdef WUF
  67. short wuf[RECLEN];
  68. #endif
  69.  
  70. switch(which) {
  71. case 0:
  72. /******************************************************************
  73. *    Read From Input File
  74. *******************************************************************/
  75.  
  76.  
  77. /*    Input Samples    */
  78.  
  79.     k = fread(buf, sizeof(buf[0]), length, chan);
  80.     if(k < length) *eof = END;
  81.     for(i=0;i<k;i++)    {
  82. #ifdef ULAW
  83.         speech[i] = (float)audio_u2s(buf[i])/32768;
  84. #else
  85.         speech[i] = (float)buf[i]/32768;
  86. #endif
  87.     }
  88.  
  89.  
  90. break;
  91.  
  92. case 1:
  93. /******************************************************************
  94. *    Write To Output File
  95. *******************************************************************/
  96.  
  97.  
  98.  
  99. /*    Output Samples    */
  100.  
  101.     for(i=0;i<length;i++)
  102. #ifndef WUF
  103. #ifdef ULAW
  104.         buf[i] = audio_s2u((short) mmax(-32768., mmin(32768.*speech[i], 32767.)));
  105. #else
  106.         buf[i] = mmax(-32768., mmin(32768.*speech[i], 32767.));
  107. #endif
  108.     k = fwrite(buf, sizeof(buf[0]), length, chan);
  109. #else
  110. wuf[i] = mmax(-32768., mmin(32768.*speech[i], 32767.));
  111. k = fwrite(wuf, sizeof(wuf[0]), length, chan);
  112. #endif
  113.     if (k != length)
  114.     {
  115.                 printf(" ***** disk write error ***** \n");
  116.         exit(1);
  117.     }
  118.  
  119. break;
  120.  
  121. case 2:
  122. /******************************************************************
  123. *    Open Files
  124. *******************************************************************/
  125.     if(chan== NULL)
  126.         switch(functn)
  127.         {
  128.             /*case READ:*/
  129.             case 0:
  130.                                 chan = fopen(fname, "rb");
  131.                 if(chan == NULL)
  132.                 {
  133.                     *eof=NOFILE;
  134.                                         printf("***** Error opening %s for reading *****\n",fname);
  135.                 }
  136.                 break;
  137.             /*case WRITE:*/
  138.             case 1:
  139.                                 chan = fopen(fname, "wb");
  140.                 if (chan == NULL)
  141.                 {
  142.                     *eof=NOFILE;
  143.                                         printf("***** Error opening %s for writing *****\n",fname);
  144.                 }
  145.                 break;
  146.         } /* end switch functn */
  147. break;
  148. case 3:
  149. /******************************************************************
  150. *    Close Files
  151. *******************************************************************/
  152. /*****
  153.     ENTRY DISKCL(CHAN)
  154.  
  155.     IF( CHAN.LT.1 .OR. CHAN.GT.MAXC ) GOTO 960
  156.     CH = CHAN
  157.  
  158. 400    IF(OPEN(CH)) THEN
  159.        CALL SPD_CLOSE(LUN(CH))
  160.        OPEN(CH) = .FALSE.
  161.     END IF
  162.     RETURN
  163. *****/
  164. printf("******** Close files not yet written ******\n");
  165. break;
  166. case 4:
  167. /******************************************************************
  168. *    Return Filenames
  169. *******************************************************************/
  170. /*****
  171.     ENTRY DISKFN(CHAN, FNAME, FLEN)
  172.  
  173.     IF( CHAN.LT.1 .OR. CHAN.GT.MAXC ) GOTO 960
  174.     IF(OPEN(CHAN)) THEN
  175.        INQUIRE(LUN(CHAN),NAME=FNAME)
  176.     ELSE
  177.            FNAME = 'None'
  178.     END IF
  179.     FLEN = LNBLNK(FNAME)
  180.     RETURN
  181.  
  182. 960     WRITE(STDERR,*) 'Invalid Channel Number: ',CHAN
  183.     RETURN
  184.  
  185.     END
  186. *****/
  187. printf("Return Filenames\n");
  188. break;
  189. } /* end switch */
  190. }
  191.